package com.example.nbshopping;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.nbshopping.Buy.HistoryActivity;
import com.example.nbshopping.Buy.ShoppingCartActivity;
import com.example.nbshopping.login.AboutActivity;
import com.example.nbshopping.login.LoginRegActivity;
import com.example.nbshopping.login.PersonCenterActivity;
import com.example.nbshopping.login.SettingActivity;
import com.example.nbshopping.login.UserBean;
import com.example.nbshopping.utils.SaveUserUtils;
import com.example.nbshopping.utils.URLUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class MeFragment extends Fragment {
    private  Button btn_cart,btn_history,btn_center,btn_about,btn_setting;
    private TextView tv_nickname;
    private ImageView headIv;
    String imgpath;
    private UserBean.DataBean userInfo;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        initView(view);
        setListener();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        setHeadview();
        isWelcome();
    }

    private void setListener() {
        btn_cart.setOnClickListener(listener);
        btn_history.setOnClickListener(listener);
        btn_center.setOnClickListener(listener);
        btn_about.setOnClickListener(listener);
        btn_setting.setOnClickListener(listener);
        headIv.setOnClickListener(listener);
    }

    private void initView(View view) {
        btn_cart=view.findViewById(R.id.frag_me_btn_cart);
        btn_history=view.findViewById(R.id.frag_me_btn_history);
        btn_center=view.findViewById(R.id.frag_me_btn_center);
        btn_about=view.findViewById(R.id.frag_me_btn_about);
        btn_setting=view.findViewById(R.id.frag_me_btn_setting);
        tv_nickname=view.findViewById(R.id.frag_me_tv_nickname);
        headIv = view.findViewById(R.id.frag_me_iv);
    }
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id=v.getId();
            switch (id){
                case R.id.frag_me_btn_cart:
                    islogin(0,1);
                    break;
                case R.id.frag_me_btn_history:
                    islogin(0,2);
                    break;
                case R.id.frag_me_btn_center:
                    islogin(0,0);
                    break;
                case R.id.frag_me_btn_about:
                    Intent intent1=new Intent(getContext(), AboutActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.frag_me_btn_setting:
                    Intent intent2=new Intent(getContext(), SettingActivity.class);
                    startActivity(intent2);
                    break;
            }
        }
    };
    //判断是否登录，如果登录就跳转到个人中心页面,如果没有登录，就跳转到登录页面
    private void islogin(int flag,int k) {//k值为0跳转个人中心，k值为1跳转购物车，k值为2跳转历史购买记录
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(getContext());
        if (userInfo==null) {
            Intent it1 = new Intent(getContext(), LoginRegActivity.class);
            it1.putExtra("flag",flag);
            startActivity(it1);
        }else if(k==0){
            Intent intent = new Intent(getContext(), PersonCenterActivity.class);
            startActivity(intent);
        }else if(k==1){
            Intent intent1=new Intent(getContext(), ShoppingCartActivity.class);
            startActivity(intent1);
        }
        else if(k==2){
            Intent intent1=new Intent(getContext(), HistoryActivity.class);
            startActivity(intent1);
        }
    }
    private void isWelcome() {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(getContext());
        if (userInfo==null) {
            tv_nickname.setText("请先登录");
        }else {
            tv_nickname.setText(userInfo.getNickname());
        }
    }

    public void setHeadview() {
        userInfo = SaveUserUtils.getUserInfo(getContext());
        if (userInfo!=null) {
            imgpath = URLUtils.getImgPath(getContext(),userInfo.getPhone());
            File file = new File(imgpath);
            if (file.exists()) {
                Bitmap bitmap = BitmapFactory.decodeFile(imgpath);
                headIv.setImageBitmap(bitmap);
            }else{
                headIv.setImageResource(R.mipmap.icon);
            }
        }else{
            headIv.setImageResource(R.mipmap.icon);
        }
        if (userInfo!=null) {
            String nickname = userInfo.getNickname();
            tv_nickname.setText(nickname);
        }else{
            tv_nickname.setText("请先登录");
        }
    }


}